package com.lsmgc.cms.util.page;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表分页。包含list属性。
 */
public class Pagination extends SimplePage {

    public Pagination() {
    }

    /**
     * 构造器
     *
     * @param pageNo     页码
     * @param pageSize   每页几条数据
     * @param totalCount 总共几条数据
     */
    public Pagination(int pageNo, int pageSize, int totalCount) {
        super(pageNo, pageSize, totalCount);
//        xsPageView();
        smPageView();
//        pageView();
    }

    /**
     * 构造器
     *
     * @param pageNo     页码
     * @param pageSize   每页几条数据
     * @param totalCount 总共几条数据
     * @param list       分页内容
     */
    public Pagination(int pageNo, int pageSize, int totalCount, List<?> list) {
        super(pageNo, pageSize, totalCount);
        this.list = list;
    }

    /**
     * 第一条数据位置
     *
     * @return
     */
    public int getFirstResult() {
        return (pageNo - 1) * pageSize;
    }

    /**
     * 当前页的数据
     */
    private List<?> list;

    /**
     * 当前页的分页样式
     */
    private List<String> pageView;

    /**
     * 获得分页内容
     *
     * @return
     */
    public List<?> getList() {
        return list;
    }

    /**
     * 设置分页内容
     *
     * @param list
     */
    @SuppressWarnings("unchecked")
    public void setList(List list) {
        this.list = list;
    }

    /**
     * 获得分页样式
     *
     * @return
     */
    public List<String> getPageView() {
        return pageView;
    }

    /**
     * 设置分页样式
     *
     * @param pageView
     */
    public void setPageView(List<String> pageView) {
        this.pageView = pageView;
    }

    /**
     * 分页显示样示部分
     */
    public void xsPageView() {

        pageView = new ArrayList<String>();

        if (this.pageNo != 1) {
            pageView.add("<input type=\"button\" value=\"首页\" onclick=\"setPageNo(1)\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"&#8249;\" onclick=\"setPageNo(" + (this.pageNo - 1) + ")\" class=\"xs\">");
        } else {
            pageView.add("<input type=\"button\" value=\"首页\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"&#8249;\" class=\"xs\">");
        }

        if (this.getTotalPage() <= 10) {
            for (int i = 0; i < this.getTotalPage(); i++) {
                if ((i + 1) == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"xs active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\" class=\"xs\">");
            }
        } else if (this.getTotalPage() <= 20) {
            //没有把...加上
            int l = 0;
            int r = 0;
            if (this.pageNo < 5) {
                l = this.pageNo - 1;
                r = 10 - l - 1;
            } else if (this.getTotalPage() - this.pageNo < 5) {
                r = this.getTotalPage() - this.pageNo;
                l = 10 - 1 - r;
            } else {
                l = 4;
                r = 5;
            }
            int tmp = this.pageNo - l;
            for (int i = tmp; i < tmp + 10; i++) {
                if (i == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"xs active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i) + "\" onclick=\"setPageNo(" + (i) + ")\" class=\"xs\">");
            }

        } else if (this.pageNo < 7) {
            for (int i = 0; i < 8; i++) {
                if (i + 1 == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"xs active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\" class=\"xs\">");
            }
            pageView.add("<input type=\"button\" value=\"...\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage() - 1) + "\" onclick=\"setPageNo(" + (this.getTotalPage() - 1) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage()) + "\" onclick=\"setPageNo(" + (this.getTotalPage()) + ")\" class=\"xs\">");
        } else if (this.pageNo > this.getTotalPage() - 6) {
            pageView.add("<input type=\"button\" value=\"" + (1) + "\" onclick=\"setPageNo(" + (1) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"" + (2) + "\" onclick=\"setPageNo(" + (2) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"...\" class=\"xs\">");
            for (int i = this.getTotalPage() - 8; i < this.getTotalPage(); i++) {
                if (i + 1 == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"xs active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\" class=\"xs\">");
            }
        } else {
            pageView.add("<input type=\"button\" value=\"" + (1) + "\" onclick=\"setPageNo(" + (1) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"" + (2) + "\" onclick=\"setPageNo(" + (2) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"...\" class=\"xs\">");

            pageView.add("<input type=\"button\" value=\"" + (this.pageNo - 2) + "\" onclick=\"setPageNo(" + (this.pageNo - 2) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo - 1) + "\" onclick=\"setPageNo(" + (this.pageNo - 1) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" class=\"xs active\" value=\"" + this.pageNo + "\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo + 1) + "\" onclick=\"setPageNo(" + (this.pageNo + 1) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo + 2) + "\" onclick=\"setPageNo(" + (this.pageNo + 2) + ")\" class=\"xs\">");

            pageView.add("<input type=\"button\" value=\"...\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage() - 1) + "\" onclick=\"setPageNo(" + (this.getTotalPage() - 1) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage()) + "\" onclick=\"setPageNo(" + (this.getTotalPage()) + ")\" class=\"xs\">");
        }
        if (this.pageNo != this.getTotalPage()) {
            pageView.add("<input type=\"button\" value=\"&#8250;\" onclick=\"setPageNo(" + (this.pageNo + 1) + ")\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"尾页\" onclick=\"setPageNo(" + this.getTotalPage() + ")\" class=\"xs\">");
        } else {
            pageView.add("<input type=\"button\" value=\"&#8250;\" class=\"xs\">");
            pageView.add("<input type=\"button\" value=\"尾页\" class=\"xs\">");
        }
        pageView.add("共" + getTotalPage() + "页 到第 <input type=\"text\" class=\"xs wpx80\" id=\"page\" class=\"xs\"> 页");
        pageView.add("<input type=\"button\" value=\"跳转\" class=\"xs\" onclick=\"setPageNo($('#page').val())\">");
    }

    /**
     * 分页显示样示部分
     */
    public void smPageView() {

        pageView = new ArrayList<String>();

        if (this.pageNo != 1) {
            pageView.add("<input type=\"button\" value=\"首页\" onclick=\"setPageNo(1)\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"&#8249;\" onclick=\"setPageNo(" + (this.pageNo - 1) + ")\" class=\"sm\">");
        } else {
            pageView.add("<input type=\"button\" value=\"首页\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"&#8249;\" class=\"sm\">");
        }

        if (this.getTotalPage() <= 10) {
            for (int i = 0; i < this.getTotalPage(); i++) {
                if ((i + 1) == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"sm active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\" class=\"sm\">");
            }
        } else if (this.getTotalPage() <= 20) {
            //没有把...加上
            int l = 0;
            int r = 0;
            if (this.pageNo < 5) {
                l = this.pageNo - 1;
                r = 10 - l - 1;
            } else if (this.getTotalPage() - this.pageNo < 5) {
                r = this.getTotalPage() - this.pageNo;
                l = 10 - 1 - r;
            } else {
                l = 4;
                r = 5;
            }
            int tmp = this.pageNo - l;
            for (int i = tmp; i < tmp + 10; i++) {
                if (i == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"sm active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i) + "\" onclick=\"setPageNo(" + (i) + ")\" class=\"sm\">");
            }

        } else if (this.pageNo < 7) {
            for (int i = 0; i < 8; i++) {
                if (i + 1 == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"sm active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\" class=\"sm\">");
            }
            pageView.add("<input type=\"button\" value=\"...\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage() - 1) + "\" onclick=\"setPageNo(" + (this.getTotalPage() - 1) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage()) + "\" onclick=\"setPageNo(" + (this.getTotalPage()) + ")\" class=\"sm\">");
        } else if (this.pageNo > this.getTotalPage() - 6) {
            pageView.add("<input type=\"button\" value=\"" + (1) + "\" onclick=\"setPageNo(" + (1) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"" + (2) + "\" onclick=\"setPageNo(" + (2) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"...\" class=\"sm\">");
            for (int i = this.getTotalPage() - 8; i < this.getTotalPage(); i++) {
                if (i + 1 == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"sm active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\" class=\"sm\">");
            }
        } else {
            pageView.add("<input type=\"button\" value=\"" + (1) + "\" onclick=\"setPageNo(" + (1) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"" + (2) + "\" onclick=\"setPageNo(" + (2) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"...\" class=\"sm\">");

            pageView.add("<input type=\"button\" value=\"" + (this.pageNo - 2) + "\" onclick=\"setPageNo(" + (this.pageNo - 2) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo - 1) + "\" onclick=\"setPageNo(" + (this.pageNo - 1) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" class=\"sm active\" value=\"" + this.pageNo + "\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo + 1) + "\" onclick=\"setPageNo(" + (this.pageNo + 1) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo + 2) + "\" onclick=\"setPageNo(" + (this.pageNo + 2) + ")\" class=\"sm\">");

            pageView.add("<input type=\"button\" value=\"...\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage() - 1) + "\" onclick=\"setPageNo(" + (this.getTotalPage() - 1) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage()) + "\" onclick=\"setPageNo(" + (this.getTotalPage()) + ")\" class=\"sm\">");
        }
        if (this.pageNo != this.getTotalPage()) {
            pageView.add("<input type=\"button\" value=\"&#8250;\" onclick=\"setPageNo(" + (this.pageNo + 1) + ")\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"尾页\" onclick=\"setPageNo(" + this.getTotalPage() + ")\" class=\"sm\">");
        } else {
            pageView.add("<input type=\"button\" value=\"&#8250;\" class=\"sm\">");
            pageView.add("<input type=\"button\" value=\"尾页\" class=\"sm\">");
        }
        pageView.add("共" + getTotalPage() + "页 到第 <input type=\"text\" class=\"sm wpx80\" id=\"page\" class=\"sm\"> 页");
        pageView.add("<input type=\"button\" value=\"跳转\" class=\"sm\" onclick=\"setPageNo($('#page').val())\">");
    }

    /**
     * 分页显示样示部分
     */
    public void pageView() {

        pageView = new ArrayList<String>();

        if (this.pageNo != 1) {
            pageView.add("<input type=\"button\" value=\"首页\" onclick=\"setPageNo(1)\">");
            pageView.add("<input type=\"button\" value=\"&#8249;\" onclick=\"setPageNo(" + (this.pageNo - 1) + ")\">");
        } else {
            pageView.add("<input type=\"button\" value=\"首页\">");
            pageView.add("<input type=\"button\" value=\"&#8249;\">");
        }

        if (this.getTotalPage() <= 10) {
            for (int i = 0; i < this.getTotalPage(); i++) {
                if ((i + 1) == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\">");
            }
        } else if (this.getTotalPage() <= 20) {
            //没有把...加上
            int l = 0;
            int r = 0;
            if (this.pageNo < 5) {
                l = this.pageNo - 1;
                r = 10 - l - 1;
            } else if (this.getTotalPage() - this.pageNo < 5) {
                r = this.getTotalPage() - this.pageNo;
                l = 10 - 1 - r;
            } else {
                l = 4;
                r = 5;
            }
            int tmp = this.pageNo - l;
            for (int i = tmp; i < tmp + 10; i++) {
                if (i == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i) + "\" onclick=\"setPageNo(" + (i) + ")\">");
            }

        } else if (this.pageNo < 7) {
            for (int i = 0; i < 8; i++) {
                if (i + 1 == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\">");
            }
            pageView.add("<input type=\"button\" value=\"...\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage() - 1) + "\" onclick=\"setPageNo(" + (this.getTotalPage() - 1) + ")\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage()) + "\" onclick=\"setPageNo(" + (this.getTotalPage()) + ")\">");
        } else if (this.pageNo > this.getTotalPage() - 6) {
            pageView.add("<input type=\"button\" value=\"" + (1) + "\" onclick=\"setPageNo(" + (1) + ")\">");
            pageView.add("<input type=\"button\" value=\"" + (2) + "\" onclick=\"setPageNo(" + (2) + ")\">");
            pageView.add("<input type=\"button\" value=\"...\">");
            for (int i = this.getTotalPage() - 8; i < this.getTotalPage(); i++) {
                if (i + 1 == this.pageNo) {
                    pageView.add("<input type=\"button\" class=\"active\" value=\"" + this.pageNo + "\">");
                    i = i + 1;
                    if (this.pageNo == this.getTotalPage()) break;
                }
                pageView.add("<input type=\"button\" value=\"" + (i + 1) + "\" onclick=\"setPageNo(" + (i + 1) + ")\">");
            }
        } else {
            pageView.add("<input type=\"button\" value=\"" + (1) + "\" onclick=\"setPageNo(" + (1) + ")\">");
            pageView.add("<input type=\"button\" value=\"" + (2) + "\" onclick=\"setPageNo(" + (2) + ")\">");
            pageView.add("<input type=\"button\" value=\"...\">");

            pageView.add("<input type=\"button\" value=\"" + (this.pageNo - 2) + "\" onclick=\"setPageNo(" + (this.pageNo - 2) + ")\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo - 1) + "\" onclick=\"setPageNo(" + (this.pageNo - 1) + ")\">");
            pageView.add("<input type=\"button\" class=\"active\" value=\"" + this.pageNo + "\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo + 1) + "\" onclick=\"setPageNo(" + (this.pageNo + 1) + ")\">");
            pageView.add("<input type=\"button\" value=\"" + (this.pageNo + 2) + "\" onclick=\"setPageNo(" + (this.pageNo + 2) + ")\">");

            pageView.add("<input type=\"button\" value=\"...\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage() - 1) + "\" onclick=\"setPageNo(" + (this.getTotalPage() - 1) + ")\">");
            pageView.add("<input type=\"button\" value=\"" + (this.getTotalPage()) + "\" onclick=\"setPageNo(" + (this.getTotalPage()) + ")\">");
        }
        if (this.pageNo != this.getTotalPage()) {
            pageView.add("<input type=\"button\" value=\"&#8250;\" onclick=\"setPageNo(" + (this.pageNo + 1) + ")\">");
            pageView.add("<input type=\"button\" value=\"尾页\" onclick=\"setPageNo(" + this.getTotalPage() + ")\">");
        } else {
            pageView.add("<input type=\"button\" value=\"&#8250;\">");
            pageView.add("<input type=\"button\" value=\"尾页\">");
        }
        pageView.add("共" + getTotalPage() + "页 到第 <input type=\"text\" class=\"sm wpx80\" id=\"page\"> 页");
        pageView.add("<input type=\"button\" value=\"跳转\" onclick=\"setPageNo($('#page').val())\">");
    }
}